package com.jic.order.impl;

import com.alibaba.fastjson.JSON;
import com.jic.order.utils.MD5Utils;
import com.jic.common.utils.HttpClientUtils;
import com.jic.order.entity.Order;
import com.jic.order.entity.OrderBale;
import com.jic.order.entity.OrderBaleDelivery;
import com.jic.order.mapper.OrderBaleDeliveryMapper;
import com.jic.order.mapper.OrderBaleMapper;
import com.jic.order.mapper.OrderMapper;
import com.jic.order.service.OrderLogisticsService;
import com.jic.util.DateUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单售后申请 服务类
 *
 * @author xuyongliang
 * @since 2020-02-01
 */
@Service
@Slf4j
public class OrderLogisticsServiceServiceImpl implements OrderLogisticsService {

	@Autowired
	private OrderBaleMapper orderBaleMapper;
	@Autowired
	private OrderBaleDeliveryMapper orderBaleDeliveryMapper;
	@Autowired
	private OrderMapper orderMapper;

	/**
	 * @Author: Puxigui
	 * @param: param 对象参数
	 * @Description: 根据订单号查询物流信息
	 * @Date: 0:20 2020/11/23
	*/
	@SneakyThrows
	@Override
	public void queryLogisticsInfo(){
		//查询所有已发货的订单信息
		Order order=new Order();
		order.setOrderStatus(4);
		List<Order> orders = orderMapper.query(order);
		if(CollectionUtils.isNotEmpty(orders)){
			for(Order orderRes:orders){
				OrderBale orderBale=new OrderBale();
				orderBale.setOrderNo(orderRes.getOrderNo());
				List<OrderBale> query = orderBaleMapper.query(orderBale);
				if(CollectionUtils.isNotEmpty(query)){
					Map<String,Object> params=new HashMap<>();
					params.put("customer","CCF162C59D2B2C772313083B14EC56A7");
					Map<String,Object> paramsBodys=new HashMap<>();
					paramsBodys.put("com",query.get(0).getDistributorCode());
					paramsBodys.put("num",query.get(0).getExpressNo());
					params.put("param",JSON.toJSONString(paramsBodys));
					params.put("sign", MD5Utils.md5(JSON.toJSONString(paramsBodys)+"XjtPreMk8235"+"CCF162C59D2B2C772313083B14EC56A7","UTF-8").toUpperCase());
					try {
						String res = HttpClientUtils.doPost("https://poll.kuaidi100.com/poll/query.do", params);
						if(StringUtils.isNotEmpty(res)){
							Map<String,Object> resMap = JSON.parseObject(res, Map.class);
							if(null!=resMap.get("status") && resMap.get("status").toString().equals("200")){
								//获取物流信息
								List<Map> mapList = JSON.parseArray(resMap.get("data").toString(), Map.class);
								//删除订单物流信息，重新插入
								OrderBaleDelivery orderBaleDelivery=new OrderBaleDelivery();
								orderBaleDelivery.setOrderNo(orderRes.getOrderNo());
								orderBaleDeliveryMapper.deleteByNo(orderBaleDelivery);
								List<OrderBaleDelivery> addParamList=new ArrayList();
								//重新批量插入
								if(CollectionUtils.isNotEmpty(mapList)){
									for(Map mapParam:mapList){
										OrderBaleDelivery orderBaleDeliveryParam=new OrderBaleDelivery();
										orderBaleDeliveryParam.setOrderNo(orderRes.getOrderNo());
										orderBaleDeliveryParam.setExpressNo(query.get(0).getExpressNo());
										orderBaleDeliveryParam.setType(1);
										orderBaleDeliveryParam.setRemark(mapParam.get("context").toString());
										orderBaleDeliveryParam.setLogisticsTime(DateUtil.getDate(mapParam.get("ftime").toString()));
										orderBaleDeliveryParam.setAvailable(1);
										orderBaleDeliveryParam.setDeliveryCompanyId(query.get(0).getDistributorId());
										orderBaleDeliveryParam.setDeliveryCompanyName(query.get(0).getDistributorName());
										addParamList.add(orderBaleDeliveryParam);
									}
									//批量插入
									orderBaleDeliveryMapper.addBaleDelivery(addParamList);
								}
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						log.error("同步单号"+orderRes.getOrderNo()+":物流信息异常");
					}
				}
			}
		}
	}
}